<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="engineer.automatapp.automat.Automat" %>
   <%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="engineer.automatapp.tools.Connector" %>
<%
final int id=1;

Automat automat = new Automat();
automat.setId(id);
int machineID = automat.getId();
Connection connection = new Connector().getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery("select machines.name, dictionary_trademarks.name, machines_trademarks.trademark_id, machines_trademarks.current_amount from automats.machines_trademarks join automats.machines on machines.id=machines_trademarks.machine_id join automats.dictionary_trademarks on dictionary_trademarks.id=machines_trademarks.trademark_id where machines.id="+automat.getId());
//Statement statement2 = connection.createStatement();
//ResultSet result2 = statement2.executeQuery("select machines.name, dictionary_trademarks.name, machines_trademarks.trademark_id, machines_trademarks.current_amount from automats.machines_trademarks join automats.machines on machines.id=machines_trademarks.machine_id join automats.dictionary_trademarks on dictionary_trademarks.id=machines_trademarks.trademark_id where machines.id="+automat.getId());
%>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<center><h1>Produkty do zakupienia</h1>

<div id="table"></div>

<br>
Wybierz produkt:
<select id="trademark">
<option value="0">Wybierz produkt...</option>
<%

while(result.next()){
int trademarkId = result.getInt("machines_trademarks.trademark_id");
String trademarkName = result.getString("dictionary_trademarks.name"); 
%>
<option value="<%=trademarkId %>"><%=trademarkName %></option>
<% 
} 
result.close();
connection.close();%>
</select>
<input type="hidden" id="machineID" value=<%=machineID %> />
<br>
<input type="submit" value="Kup" id="refresh" /><br>
<div id="message"></div>
    </center>
<script type="text/javascript">

this.getXMLHttpRequest = function(){
	var request=false;

    try{
        request= new XMLHttpRequest();
    }catch(err1){
        try{
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }catch(err2){
            try{
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }catch(err3){
                request=false;
            }
        }
    }
    $.extend(request, { isBuyAction: false });
    $.extend(request, { targetObject: null });
    return request;
}

this.processResponse = function(){
    if(this.readyState==4){
        if(this.status==200){
        	console.log(this.targetObject);
        	this.targetObject.html(this.responseText);
            if( this.isBuyAction ) {
            	new RequestHandler().refresh();  
            }
        };
    }
}

function RequestHandler() {
	
	this.status = null;
	this.r = null;
	
	this.refresh = function(){
    	var status = $("#table");
    	var r = getXMLHttpRequest();
    	
    	var machineID = document.getElementById("machineID").value;
    	
    	r.targetObject = status;
    	
    	r.open("GET", "SendToRefreshServlet?machineID=" + machineID, true);       
    	r.onreadystatechange = processResponse;
    	r.send(null);
	
	}
	// >:/
	this.buy = function(){
	    var status = $("#message");
	    var r = getXMLHttpRequest();
	    
	    var trademarkID = document.getElementById("trademark").value;
		var machineID = document.getElementById("machineID").value;
	    
		r.open("GET", "SendToBuyServlet?trademarkID="+trademarkID+"&machineID="+machineID, true);
		r.isBuyAction = true;
		r.targetObject = status;
	    r.onreadystatechange = processResponse;
	    r.send(null);
	}

	this.sendRequest = function(){
	    var status = $("#message");
	    var r = getXMLHttpRequest();
	    
	    var machineID = document.getElementById("machineID").value;
	    
	    r.open("GET", "SendToRequestServlet?machineID="+machineID, true);
	    r.targetObject = status;
	    
	    r.onreadystatechange = processResponse;
	    r.send(null);
	}
}

</script>

<script type="text/javascript">
	var reportMaster = new RequestHandler();
	reportMaster.refresh();
	reportMaster.sendRequest();
	setInterval(reportMaster.sendRequest, 300000);
	$('#refresh').live('click', function(){new RequestHandler().buy();});
</script>
